# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Francesco Colista <fcolista@alpinelinux.org>
# Maintainer: Francesco Colista <fcolista@alpinelinux.org>
pkgname=bareos
pkgver=19.2.8
pkgrel=0
pkgdesc="Bareos - Backup Archiving REcovery Open Sourced"
url="http://www.bareos.org"
# FTBFS on mips
arch="all !mips !mips64"
options="!check"
license="AGPL-3.0"
makedepends="cmake gawk
lzo-dev acl-dev gettext-dev
readline-dev ncurses-dev jansson-dev
sqlite-dev postgresql-dev mariadb-connector-c-dev
php7"
install="$pkgname.pre-install $pkgname.post-install"
subpackages="$pkgname-dev $pkgname-doc $pkgname-openrc $pkgname-dbg
$pkgname-mysql:_mysql $pkgname-sqlite:_sqlite $pkgname-postgresql:_postgresql
$pkgname-webui:_webui:noarch $pkgname-webui-apache2:_webui_apache2:noarch $pkgname-webui-nginx:_webui_nginx:noarch"
pkgusers=$pkgname
pkggroups=$pkgname
source="$pkgname-$pkgver.tar.gz::https://github.com/bareos/bareos/archive/Release/$pkgver.tar.gz
	$pkgname-dir.initd
	$pkgname-sd.initd
	$pkgname-fd.initd
	detect-alpine.patch
	add-libintl.patch
	nginx-conf.patch
	path-mounted.patch
	"
builddir="$srcdir"/$pkgname-Release-$pkgver

# secfixes:
#   19.2.8-r0:
#     - CVE-2020-4042
#     - CVE-2020-11061

prepare() {
	default_prepare
	mkdir -pv core/platforms/alpine
	touch core/platforms/alpine/CMakeLists.txt

	# cleanup stale builddir and recreate
	[ -d cmake-build ] && rm -rfv cmake-build
	mkdir cmake-build
}

build() {
	cd "$builddir"/cmake-build

	cmake .. \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DCMAKE_INSTALL_PREFIX:PATH=/usr \
		-DCMAKE_INSTALL_LIBDIR:PATH=/usr/lib \
		-DBUILD_SHARED_LIBS:BOOL=ON \
		-Dsysconfdir=/etc \
		-Dbasename=$pkgname \
		-Dhostname=localhost \
		-Dconfdir=/etc/$pkgname \
		-Dlibdir=/usr/lib \
		-Darchivedir=/var/lib/$pkgname/archive \
		-Dworkingdir=/var/lib/$pkgname \
		-Dscriptdir=/etc/$pkgname/scripts \
		-Dplugindir=/usr/lib/$pkgname \
		-Dbackenddir=/usr/lib/$pkgname \
		-Dlogdir=/var/log/$pkgname \
		-Dpiddir=/run/$pkgname \
		-Dbsrdir=/run/$pkgname \
		-Dsubsysdir=/var/lock \
		-Dsmartalloc=yes \
		-Dbatch-insert=yes \
		-Dscsi-crypto=yes \
		-Dlmdb=yes \
		-Dipv6=yes \
		-Ddynamic-cats-backends=yes \
		-Dpostgresql=yes \
		-Dmysql=yes \
		-Dsqlite3=yes \
		-Dopenssl=yes \
		-Dtraymonitor=no \
		-Dsystemd=no \
		-Dndmp=no \
		-Ddir-user=$pkgname \
		-Ddir-group=$pkgname \
		-Dsd-user=$pkgname \
		-Dsd-group=$pkgname \
		-Dfd-user=root \
		-Dfd-group=$pkgname

	make
}

package() {
	cd "$builddir"/cmake-build
	make DESTDIR="$pkgdir" install
	mkdir -p "$pkgdir"/usr/sbin
	mkdir -p "$pkgdir"/usr/bin
	mkdir -p "$pkgdir"/var/lib/bareos
	mkdir -p "$pkgdir"/var/log/bareos

	local daemon; for daemon in dir sd fd; do
		install -Dm755 "$srcdir"/$pkgname-$daemon.initd \
			"$pkgdir"/etc/init.d/$pkgname-$daemon
	done

	# Install logrotate script
	install -Dm644 ../core/scripts/logrotate "$pkgdir"/etc/logrotate.d/$pkgname

	# Fix job to use shell script instead of perl script
	sed -i -e 's/make_catalog_backup.pl/make_catalog_backup/' \
		"$pkgdir"/etc/bareos/bareos-dir.d/job/BackupCatalog.conf

	# remove perl script
	rm "$pkgdir"/etc/bareos/scripts/make_catalog_backup.pl

	# remove http configuration
	rm -r "$pkgdir"/etc/httpd
}

_mysql() {
	description="$description (mysql driver)"
	mkdir -p "$subpkgdir"/usr/lib/bareos
	mv "$pkgdir"/usr/lib/bareos/libbareoscats-mysql.so "$subpkgdir"/usr/lib/bareos/

	for _i in creates grants updates; do
		mkdir -p "$subpkgdir"/etc/bareos/scripts/ddl/$_i
		mv "$pkgdir"/etc/bareos/scripts/ddl/$_i/mysql*.sql "$subpkgdir"/etc/bareos/scripts/ddl/$_i/
	done
}

_sqlite() {
	description="$description (sqlite driver)"
	mkdir -p "$subpkgdir"/usr/lib/bareos
	mv "$pkgdir"/usr/lib/bareos/libbareoscats-sqlite3.so "$subpkgdir"/usr/lib/bareos/

	for _i in creates updates; do
		mkdir -p "$subpkgdir"/etc/bareos/scripts/ddl/$_i
		mv "$pkgdir"/etc/bareos/scripts/ddl/$_i/sqlite3*.sql "$subpkgdir"/etc/bareos/scripts/ddl/$_i/
	done
}

_postgresql() {
	description="$description (postgresql driver)"
	mkdir -p "$subpkgdir"/usr/lib/bareos
	mv "$pkgdir"/usr/lib/bareos/libbareoscats-postgresql.so "$subpkgdir"/usr/lib/bareos/

	for _i in creates grants updates; do
		mkdir -p "$subpkgdir"/etc/bareos/scripts/ddl/$_i
		mv "$pkgdir"/etc/bareos/scripts/ddl/$_i/postgresql*.sql "$subpkgdir"/etc/bareos/scripts/ddl/$_i/
	done
}

_webui() {
	description="$description (webui)"
	depends="php7 php7-gettext php7-mbstring php7-session php7-ctype php7-openssl php7-json"

	mkdir -p "$subpkgdir"/usr/share
	mv "$pkgdir"/bareos-webui "$subpkgdir"/usr/share/

	mkdir -p "$subpkgdir"/etc
	mv "$pkgdir"/etc/bareos-webui "$subpkgdir"/etc/

	mkdir -p "$subpkgdir"/etc/bareos/bareos-dir.d/console/
	mv "$pkgdir"/etc/bareos/bareos-dir.d/console/admin.conf.example "$subpkgdir"/etc/bareos/bareos-dir.d/console/

	mkdir -p "$subpkgdir"/etc/bareos/bareos-dir.d/profile/
	mv "$pkgdir"/etc/bareos/bareos-dir.d/profile/webui-admin.conf "$subpkgdir"/etc/bareos/bareos-dir.d/profile/
}

_webui_apache2() {
	description="$description (webui apache2 configuration)"
	depends="$pkgname-webui php7-apache2"

	mkdir -p "$subpkgdir"/etc/apache2/conf.d
	cp "$builddir"/webui/install/apache/bareos-webui.conf "$subpkgdir"/etc/apache2/conf.d/
}


_webui_nginx() {
	description="$description (webui apache2 configuration)"
	depends="$pkgname-webui nginx"

	mkdir -p "$subpkgdir"/etc/nginx/conf.d
	cp "$builddir"/webui/install/nginx/bareos-webui.conf "$subpkgdir"/etc/nginx/conf.d/
}


sha512sums="60bbbbd949a63f978cf1ed7d0cfc22c22f117d8c2700eb47531e15b2d91fc700c522519f0b816cd1040283f09b4c81f36910c66c8b6f4bf1ff22180e979bdeb5  bareos-19.2.8.tar.gz
eb1e7072b579bf9ae21f2e351d6900abb277db64e373f4760bac8188b82929376e4a196d2c935cefe1ae4cc2c396f2fcba1a25642b26e2f92a0d008fbdc4b5f2  bareos-dir.initd
c770b1d041fafef93d4eb0269ba8d9733e85ef465657fe8dd5d5c68a27ec773cec9c5c582d4a16596d95bbf6dbd3f7194dc9c0d8ed73138e9fb438fba9aa9445  bareos-sd.initd
c6347079dbcef5f4a69ec0c4ecc31803520d715d599d89c6bbfbb3741a86c50d7295c30432889b13ee9c16f2feaa84b1c6ae992cfee6505d569c6493d7e85a5b  bareos-fd.initd
1251d81d142b58bb2f5ab2ea762068fe2273aa5face9dfd314011706e9c2189ecab36b80f76317a595679f164b124cff85623c1ec98cc6997e350b6223f2a524  detect-alpine.patch
9d12f26d77d384ab122fdb133667b17a1e83c748d9fca9ca810c368b8f4f3cb2fc258f7c54628e96a0347e7f996c00f46d89655aad2fee373a61e7433fd783b9  add-libintl.patch
c2fd44d5880b74b24d8001b0798a1a42936347284633b9dd93d604a642c7b15220e5af66cbceb8919263e85b3cfee866cf3f956046ab7cc0d93edce96ad1ecda  nginx-conf.patch
2053eb72bbdb8c02d9ee7dcf387a973e0203485eb85304f1e1513ad501f6738d9fc0bb97596d6d9a1a8c96f6675fd2d2edafc85e961a5bd370b5f22f320c7cbf  path-mounted.patch"
